//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>
#include "timerint.h"

INT_32 TstIndex = 0;
INT_32 EndValue[TSTTIMES];

INT_32 g_nMaxLatency = 0xffff;
INT_32 g_nTemp = 0;

INT_32 TstIndexBak;
INT_32 g_nMaxLatencyBak;

UNS_32 g_uFlags;

void ReadTimer3()
{
    g_nTemp = TIMER3->value;
    if (g_nMaxLatency > g_nTemp) {
        g_nMaxLatency = g_nTemp;
    }

    // Clears the TIMER3 int
    TIMER3->clear = 0;

    // TIMER3 reload
    TIMER3->load = 0xFFFF;

    TstIndex++;
    if (TstIndex == TSTTIMES) {
        TstIndexBak = TstIndex;
        g_nMaxLatencyBak = g_nMaxLatency;

        TstIndex = 0;
        g_nMaxLatency = 0xffff;

        g_event.NotifyByIsr(SIGNALED);

    }
}

void TimerIsr(irq_t irq, void *pvDevice, InterruptContext *pContext)
{
#if (_TEST_TYPE == 5) /* Test for interrupt latency performance */
    ReadTimer3();
#endif // _TEST_TYPE == 5

#if (_TEST_TYPE == 6) /* Test for interrupt dispatch latency performance */
    g_uFlags = SaveFlagsAndCli();
    g_eventIST.NotifyByIsr(SIGNALED);
#endif // _TEST_TYPE == 6
}
